//
//  ZSReceiveGuideViewController.swift
//  creativeassistant
//
//  Created by 灵工邦 on 2025/2/13.
//

import UIKit
import StoreKit
import Photos

class ZSReceiveGuideViewController: ZSBaseViewController {
    
    var receiveHandler: ((Bool) -> Void)?
    
    @IBOutlet weak var screenshotbBtn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(hexString: "#000000", alpha: 0.3)
    }
    
    @IBAction func dismiss(_ sender: UITapGestureRecognizer) {
        self.dismiss(animated: false)
    }
    
    @IBAction func uploadScreenshot(_ sender: UIButton) {
        presentImagePicker(sourceType:.photoLibrary)
    }
    
    func presentImagePicker(sourceType: UIImagePickerController.SourceType) {
        DispatchQueue.main.async {
            let imagePicker = UIImagePickerController()
            imagePicker.sourceType = sourceType
            imagePicker.delegate = self
            self.present(imagePicker, animated: true, completion: nil)
        }
    }
    
    @IBAction func goComment(_ sender: UIButton) {
        if #available(iOS 10.3, *) {
            SKStoreReviewController.requestReview()
        }
    }
    
    @IBAction func submit(_ sender: UIButton) {
        guard let _ = self.screenshotbBtn.imageView?.image else { return }
        UserDefaults.standard.set(Date(), forKey: zsBenefit)
        if let handler = receiveHandler {
            handler(true)
        }
        self.dismiss(animated: false)
        UIApplication.shared.keyWindow?.makeToast("领取成功", position: .center)
    }
}

extension ZSReceiveGuideViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let image = info[.originalImage] as? UIImage {
            self.screenshotbBtn.setImage(image, for: .normal)
        }
        picker.dismiss(animated: true, completion: nil)
    }
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }
}

